package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrRelationRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author qindewan
 * @email qindewan@gmail.com
 * @date 2021-06-28 16:12:35
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;



    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(value = "catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        System.out.println("page为1:"+catelogId);
        PageUtils page = attrGroupService.queryPageByCategoryId(params,catelogId);
        System.out.println("page为2:"+page);
        return R.ok().put("page", page);
    }

    /**
     * 查询分组下的所有属性
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R attrGroupInfo(@PathVariable("attrGroupId") Long attrGroupId){
     List<AttrEntity> attrEntity = attrgroupRelationService.getGroupAttr(attrGroupId);

        return R.ok().put("data", attrEntity);
    }

    /**
     * 保存关联关系
     */
    @RequestMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R saveAttrRelation(@RequestBody List<AttrRelationRespVo> relationRespVos){
         attrgroupRelationService.saveAttrRelation(relationRespVos);

        return R.ok();
    }


    /**
     * 查询分组下的所有属性
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:info")
    public R noattrGroupInfo(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrService.getnoGroupAttr(params,attrGroupId);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> catelogPath = categoryService.findCatelogPath(attrGroupId);
       attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody AttrRelationRespVo[] attrRelationRespVos){
		//attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        attrGroupService.removeAttrGroupRelation(attrRelationRespVos);

        return R.ok();
    }

}
